﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Web.Script;
using System.Web.Services;
using System.ComponentModel;
using System.Web.Script.Services;
using Ext.Net;
using Ent.Inscripcion.Core;
using Ent.Inscripcion.Core.Core;

namespace Ent.Inscripcion.Presentacion.Administracion
{
    public partial class Catalogos : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnBuscar_Click(object sender, DirectEventArgs e)
        {
            try
            {
                var catalogoCriterio = CatalogoCriterio.NewCatalogoCriterio();
                catalogoCriterio.Codigo = e.ExtraParams["Codigo"];
                catalogoCriterio.Nombre = e.ExtraParams["Nombre"];
                sCatalogos.DataSource = CatalogoLista.GetCatalogoLista(catalogoCriterio);
                sCatalogos.DataBind();

            }
            catch (Exception ex)
            {
                Util.RegistarLog(ex);
            }
        }
        /// <summary>
        /// Permite manejar la accion realizada sobre un Catalogo
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ManejarAccion(object sender, DirectEventArgs e)
        {
            try
            {
                Guid idCatalogo = Guid.Parse(e.ExtraParams["Id"]);
                string opcion = e.ExtraParams["Comando"];
                var catalogo = Catalogo.Get(idCatalogo);
                txtCodigoNuevoCatalogo.Text = catalogo.Codigo;
                txtNombre.Text = catalogo.Nombre;
                txaDescripcionCatalogo.Text = catalogo.Descripcion;

                var datos = catalogo.Items;
                if (datos != null)
                {
                    sListadoItemsCatalogo.DataSource = datos;
                    sListadoItemsCatalogo.DataBind();
                }
                switch (opcion)
                {
                    case "Eliminar":
                        hidAccionCatalogo.Text = "Eliminar";
                        btnEliminar.Show();
                        btnGuardar.Hide();
                        break;
                    case "Editar":                        
                        hidAccionCatalogo.Text = "Editar";
                        btnEliminar.Hide();
                        btnGuardar.Show();
                        break;
                    default:
                        break;
                }
                hidIdCatalogo.Text = idCatalogo.ToString();
                wItemCatalogo.Show();
                e.Success = true;
            }
            catch (Exception ex)
            {
                e.Success = false;
                Util.RegistarLog(ex);
            }
        }
        
        protected void sListaCatalogos_Refresh(object sender, DirectEventArgs e)
        {
            string id = e.ExtraParams["Id"];
            Guid catalogoId;
            catalogoId = Guid.Parse(id);
            sListaCatalogos.DataSource = CatalogoItemLista.GetListaCatalogos(catalogoId);
            sListaCatalogos.DataBind();
        }

        public void btnEliminar_Click(object sender, DirectEventArgs e)
        {
            try
            {
                string id = e.ExtraParams["Id"];
                Guid idCatalogo = Guid.Parse(id);
                var catalogo = Catalogo.Get(idCatalogo);
                catalogo.Delete();
                catalogo.Save();

                gdpItemsCatalogos.DeleteSelected();
                sCatalogos.DataSource = CatalogoLista.GetCatalogoLista(CatalogoCriterio.NewCatalogoCriterio());
                sCatalogos.DataBind();
                e.Success = true;
            }
            catch (Exception ex)
            {
                e.Success = false;
                Util.RegistarLog(ex);
            }

        }
        protected void btnGuardar_Click(object sender, DirectEventArgs e)
        {
            try
            {
                string jsonItems = e.ExtraParams["ItemsCatalogo"];
                string accion = e.ExtraParams["Accion"];
                string id = e.ExtraParams["Id"];
                var nombre = e.ExtraParams["Nombre"];
                var codigo = e.ExtraParams["Codigo"];
                var descripcion = e.ExtraParams["Descripcion"];
                Catalogo catalogo = null;
                if (accion == "Editar")
                    catalogo = Catalogo.Get(new Guid(id));
                else
                    catalogo = Catalogo.New();                

                catalogo.Codigo = codigo;
                catalogo.Nombre = nombre;
                catalogo.Descripcion = descripcion;

                StoreDataHandler sdhItems = new StoreDataHandler(jsonItems);
                ChangeRecords<CatalogoItem> crItems = sdhItems.ObjectData<CatalogoItem>();
                foreach (var item in crItems.Deleted)
                {
                    int indice = catalogo.Items.IndexOf(catalogo.Items.FirstOrDefault(x => x.Id == item.Id));
                    catalogo.Items.RemoveAt(indice);
                }

                foreach (var item in crItems.Updated)
                {
                    int indice = catalogo.Items.IndexOf(catalogo.Items.FirstOrDefault(x => x.Id == item.Id));
                    Csla.Data.DataMapper.Map(item, catalogo.Items[indice]);
                }

                foreach (var item in crItems.Created)
                {
                    var itemCatalago = CatalogoItem.NewCatalogoItem();
                    itemCatalago.Codigo = item.Codigo;
                    itemCatalago.Nombre = item.Nombre;
                    itemCatalago.Descripcion = item.Descripcion;
                    catalogo.Items.Add(itemCatalago);
                }

                catalogo = catalogo.Save();
                

                sCatalogos.DataSource = CatalogoLista.GetCatalogoLista(CatalogoCriterio.NewCatalogoCriterio());
                sCatalogos.DataBind();
                e.Success = true;
            }
            catch (Exception ex)
            {
                e.Success = false;
                Util.RegistarLog(ex);
            }
        }
    }
}